package com.example.flower_shop.service.adm.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.constans.ErrorMsg;
import com.example.flower_shop.entity.UserEntity;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.RoleMapper;
import com.example.flower_shop.mapper.UserMapper;
import com.example.flower_shop.service.adm.AdmUserService;
import com.example.flower_shop.vo.user.AdmUserPageReqVO;
import com.example.flower_shop.vo.user.AdmUserPageRespVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdmUserServiceImpl implements AdmUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public PageResult<AdmUserPageRespVO> list(AdmUserPageReqVO reqVO) {
        Page<UserEntity> page = new Page<>(reqVO.getPage(), reqVO.getSize());
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(reqVO.getNickname())) {
            wrapper.like(UserEntity::getNickname, reqVO.getNickname());
        }
        if (StringUtils.isNotBlank(reqVO.getUsername())) {
            wrapper.like(UserEntity::getUsername, reqVO.getUsername());
        }
        if (StringUtils.isNotBlank(reqVO.getPhone())) {
            wrapper.like(UserEntity::getPhone, reqVO.getPhone());
        }
        if (reqVO.getGender() != null) {
            wrapper.eq(UserEntity::getGender, reqVO.getGender());
        }
        if (reqVO.getStatus() != null) {
            wrapper.eq(UserEntity::getStatus, reqVO.getStatus());
        }
        wrapper.orderByDesc(UserEntity::getCreateTime);
        IPage<UserEntity> iPage = userMapper.selectPage(page, wrapper);
        List<AdmUserPageRespVO> res = iPage.getRecords().stream().map(user -> {
            AdmUserPageRespVO respVO = BeanUtil.copyProperties(user, AdmUserPageRespVO.class);
            //respVO.setPhone(DesensitizedUtil.mobilePhone(user.getPhone()));
            respVO.setRoleName(roleMapper.getRoleByUserId(user.getId()).getName());
            return respVO;
        }).toList();
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), res);
    }

    @Override
    public void changeStatus(Long userId) {
        UserEntity user = userMapper.selectById(userId);
        if (user == null) {
            throw new exception(ErrorMsg.USER_NOT_EXIST);
        }
        user.setStatus(!user.getStatus());
        if (userMapper.updateById(user) < 1) {
            throw new exception(ErrorMsg.UPDATE_FAILED);
        }
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return userMapper.deleteByIds(List.of(ids));
    }
}
